package de.jmda.fx.node.behaviour;

import javafx.scene.Node;
import javafx.scene.input.MouseEvent;

import org.apache.log4j.Logger;

import de.jmda.fx.node.EventHandler;

public class BehaviourHighlightingOnMouseEntered<T extends MouseEvent>
		extends BehaviourHighlighting<MouseEvent>
{
	private final static Logger LOGGER =
			Logger.getLogger(BehaviourHighlightingOnMouseEntered.class);
	
	/**
	 * highlightable object, may but does not have to be the same as {@link
	 * AbstractNodeBehaviour#node}
	 */
	private Highlightable highlightable;

	/**
	 * @param node that triggers highlighting ({@link AbstractNodeBehaviour#node})
	 * @param highlightable object to be highlighted
	 */
	public BehaviourHighlightingOnMouseEntered(
			Node node, Highlightable highlightable)
  {
	  super(node);

	  this.highlightable = highlightable;

	  javafx.event.EventHandler<? super MouseEvent> currentMouseEventHandler =
	  		node.onMouseEnteredProperty().get();
	  
	  EventHandler<MouseEvent> eventHandler;

	  if (currentMouseEventHandler == null)
	  {
	  	eventHandler = new EventHandler<MouseEvent>();
	  	eventHandler.add(this);
	  	node.onMouseEnteredProperty().set(eventHandler);
	  }
	  else if (currentMouseEventHandler instanceof EventHandler)
		{
	  	eventHandler = (EventHandler<MouseEvent>) currentMouseEventHandler;
			eventHandler.add(this);
	  	node.onMouseEnteredProperty().set(eventHandler);
	  }
		else
		{
			LOGGER.error(
					"failure adding behaviour " + getClass().getName() + " to " +
					currentMouseEventHandler.getClass().getName() + " event handler");
		}
  }

	@Override
  public void showBehaviour(MouseEvent event)
  {
		highlightable.highlightingOn();
  }
}